﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using Docsodll;
using  CrystalDecisions.Shared;
using  CrystalDecisions.CrystalReports.Engine;
using  CrystalDecisions.Windows.Forms;


namespace INHOADON_TAMVAN.BaoCao
{
    public partial class frmReportHDGTGT : Form
    {
        ParameterField paraField;
        ParameterFields paraFields = new ParameterFields();
        ParameterDiscreteValue paraDiscreteValue;
        public  void  PassParameter(string  parameterFieldName, string  parameterDiscreteValue, CrystalReportViewer  viewer)
        {
            paraField = new  ParameterField();
            paraField.Name = parameterFieldName;//tên c?a parameter
            paraDiscreteValue = new  ParameterDiscreteValue();
            paraDiscreteValue.Value = parameterDiscreteValue;//gán giá tr? cho parameter
            paraField.CurrentValues.Add(paraDiscreteValue);
            paraFields.Add(paraField);
            viewer.ParameterFieldInfo = paraFields;
        }

       
        DataClasses1DataContext db;
        Docsodll.clsDocSo obj = new Docsodll.clsDocSo();
        public delegate void reloadListHD();
        public reloadListHD reloadList;
        public frmReportHDGTGT()
        {
            db = new DataClasses1DataContext(SystemInfo.pub_ConnString);
            InitializeComponent();
        }

        public int khogiay = 10;
        public string hd_id;
        dsCTHD dsCTHDObj = new dsCTHD();
        string sotienbangchu;

        void fillKhoLon()
        {
            DataTable dact = new DataTable();

            //dact.Columns.Add("HD_ID");
            dact.Columns.Add("HH_Tenhanghoa");
            dact.Columns.Add("CTHD_Soluong");
            dact.Columns.Add("HH_DonViTinh");
            dact.Columns.Add("CTHD_DonGia", typeof(Decimal));
            dact.Columns.Add("CTHD_ThanhTien", typeof(Decimal));
            //dact.Columns.Add("HH_Dongia");

            //sét khổ giấy


            //khởi tạo report 
            INHOADON_TAMVAN.CrystalReports.RptHoaDon rptHoaDonObj = new INHOADON_TAMVAN.CrystalReports.RptHoaDon();
            var chitiethoadon = (from cthd in db.Chitiethoadons join hanghoa in db.HangHoas on cthd.CTHD_HH_ID equals hanghoa.HH_ID where cthd.CTHD_HD_ID == hd_id select new { hanghoa.HH_Tenhanghoa, cthd.CTHD_Soluong, hanghoa.HH_DonViTinh, cthd.CTHD_DonGia, cthd.CTHD_ThanhTien, cthd.CTHD_HD_ID, hanghoa.HH_Dongia }).ToList();
            for (int i = 0; i < khogiay; i++)
            {
                if (i < chitiethoadon.Count)
                {
                    dact.Rows.Add(chitiethoadon[i].HH_Tenhanghoa, chitiethoadon[i].CTHD_Soluong, chitiethoadon[i].HH_DonViTinh, chitiethoadon[i].CTHD_DonGia, chitiethoadon[i].CTHD_ThanhTien);
                }
                else
                {
                    dact.Rows.Add(null, null, null, null, null);
                }

            }

            var chiTietDocTien = from cthd in db.Chitiethoadons where cthd.CTHD_HD_ID == hd_id select new { cthd.CTHD_ThanhTien };

            sotienbangchu = obj.Num2Text(chiTietDocTien.Sum(c => c.CTHD_ThanhTien).ToString(), true);


            rptHoaDonObj.SetDataSource(dact);
            crystalReportViewer1.ReportSource = rptHoaDonObj;

            var hd1 = (from hd in db.HoaDons where hd.HD_ID == hd_id select hd).Single();

            DateTime ngayhoadon = DateTime.Parse(hd1.HD_Ngayhd.ToString());
            String strngayhoadon = @"      " + ngayhoadon.Day + @"                 " + ngayhoadon.Month + @"                   " + ngayhoadon.Year.ToString().TrimStart('2').TrimStart('0');

            this.PassParameter("paraThueSuat", hd1.HD_Thuesuat.ToString(), crystalReportViewer1);
            this.PassParameter("paraTienBangChu", sotienbangchu, crystalReportViewer1);

            this.PassParameter("paraSoHoaDon", hd1.HD_ID, crystalReportViewer1);
            this.PassParameter("paraNgayHoaDon", strngayhoadon, crystalReportViewer1);
            this.PassParameter("paraHoTenNguoiMua", hd1.KhachHang.KH_HOTEN, crystalReportViewer1);
            this.PassParameter("paraDonVi", hd1.KhachHang.KH_DONVI, crystalReportViewer1);
            this.PassParameter("paraMaSoThue", hd1.HD_Thuesuat.ToString(), crystalReportViewer1);
            this.PassParameter("paraDiaChi", hd1.KhachHang.KH_DIACHI, crystalReportViewer1);
            this.PassParameter("paraHinhThucThanhToan", hd1.HD_Hinhthucthanhtoan, crystalReportViewer1);
            this.PassParameter("paraSoTaiKhoan", hd1.KhachHang.KH_STK, crystalReportViewer1);

        }

        void fillKhoNho()
        {
            DataTable dact = new DataTable();

            //dact.Columns.Add("HD_ID");
            dact.Columns.Add("HH_Tenhanghoa");
            dact.Columns.Add("CTHD_Soluong");
            dact.Columns.Add("HH_DonViTinh");
            dact.Columns.Add("CTHD_DonGia", typeof(Decimal));
            dact.Columns.Add("CTHD_ThanhTien", typeof(Decimal));
            //dact.Columns.Add("HH_Dongia");

            //sét khổ giấy


            //khởi tạo report 
            INHOADON_TAMVAN.CrystalReports.rptHoaDon_Nho rptHoaDonObj = new INHOADON_TAMVAN.CrystalReports.rptHoaDon_Nho();
            var chitiethoadon = (from cthd in db.Chitiethoadons join hanghoa in db.HangHoas on cthd.CTHD_HH_ID equals hanghoa.HH_ID where cthd.CTHD_HD_ID == hd_id select new { hanghoa.HH_Tenhanghoa, cthd.CTHD_Soluong, hanghoa.HH_DonViTinh, cthd.CTHD_DonGia, cthd.CTHD_ThanhTien, cthd.CTHD_HD_ID, hanghoa.HH_Dongia }).ToList();
            for (int i = 0; i < 8; i++)
            {
                if (i < chitiethoadon.Count)
                {
                    dact.Rows.Add(chitiethoadon[i].HH_Tenhanghoa, chitiethoadon[i].CTHD_Soluong, chitiethoadon[i].HH_DonViTinh, chitiethoadon[i].CTHD_DonGia, chitiethoadon[i].CTHD_ThanhTien);
                }
                else
                {
                    dact.Rows.Add(null, null, null, null, null);
                }

            }

            var chiTietDocTien = from cthd in db.Chitiethoadons where cthd.CTHD_HD_ID == hd_id select new { cthd.CTHD_ThanhTien };

            sotienbangchu = obj.Num2Text(chiTietDocTien.Sum(c => c.CTHD_ThanhTien).ToString(), true);


            rptHoaDonObj.SetDataSource(dact);
            crystalReportViewer1.ReportSource = rptHoaDonObj;

            var hd1 = (from hd in db.HoaDons where hd.HD_ID == hd_id select hd).Single();

            DateTime ngayhoadon = DateTime.Parse(hd1.HD_Ngayhd.ToString());
            String strngayhoadon = @" " + ngayhoadon.Day + @"               " + ngayhoadon.Month + @"                 " + ngayhoadon.Year.ToString().TrimStart('2').TrimStart('0');

            this.PassParameter("paraThueSuat", hd1.HD_Thuesuat.ToString(), crystalReportViewer1);
            this.PassParameter("paraTienBangChu", sotienbangchu, crystalReportViewer1);

            this.PassParameter("paraSoHoaDon", hd1.HD_ID, crystalReportViewer1);
            this.PassParameter("paraNgayHoaDon", strngayhoadon, crystalReportViewer1);
            this.PassParameter("paraHoTenNguoiMua", hd1.KhachHang.KH_HOTEN, crystalReportViewer1);
            this.PassParameter("paraDonVi", hd1.KhachHang.KH_DONVI, crystalReportViewer1);
            this.PassParameter("paraMaSoThue", hd1.HD_Thuesuat.ToString(), crystalReportViewer1);
            this.PassParameter("paraDiaChi", hd1.KhachHang.KH_DIACHI, crystalReportViewer1);
            this.PassParameter("paraHinhThucThanhToan", hd1.HD_Hinhthucthanhtoan, crystalReportViewer1);
            this.PassParameter("paraSoTaiKhoan", hd1.KhachHang.KH_STK, crystalReportViewer1);

        }
        private void frmReportHDGTGT_Load(object sender, EventArgs e)
        {
            if (khogiay == 10)
            {
                fillKhoLon();
            }
            else
            {
                fillKhoNho();
            }
        }
    }
}
